home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 04 - 1988 / 04.03 Mar 88 / GLA source / GLA.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-02-02  |  14.6 KB  |  631 lines  |  [TEXT/KAHL]

  1. /*    Graphic Load Average
  2.     © 1987 Peter Korn
  3.  
  4.     This is a little program designed 
  5.     to run in the backround under MultiFinder.
  6.     It's purpose:  to graphically indicate 
  7.     the 'load' by creating a column chart of 
  8.     the load.
  9.             
  10. */
  11. /*     Defines    */
  12.  
  13. #define        NIL                     0
  14. #define     nullStopMask         0
  15. #define        MYALERT                128
  16. #define        MYDIALOG            150
  17. #define        MYWINDOW            128
  18. #define     DESK_ID                128
  19. #define        FILE_ID                129
  20. #define        EDIT_ID                130
  21. #define        CONFIGURE_ID        131
  22. #define        BASE_ID                200        /* heirarchical menus */
  23. #define        WINDOW_POSh            150
  24. #define        WINDOW_POSv            151
  25. #define        BAROFFSET            10
  26. #define        WNETrapNum            0x60
  27. #define        UnImplTrapNum        0x9F
  28. #define        SUSPEND_RESUME_EVT    15
  29. #define        SUSPEND_RESUME_BIT    0
  30. #define        CLIPBOARD_BIT        0
  31. #define        MessageDialog        258
  32.  
  33. /*     Includes    */
  34.  
  35. #include <Quickdraw.h>
  36. #include <MacTypes.h>
  37. #include <FontMgr.h>
  38. #include <WindowMgr.h>
  39. #include <MenuMgr.h>
  40. #include <TextEdit.h>
  41. #include <DialogMgr.h>
  42. #include <EventMgr.h>
  43. #include <DeskMgr.h>
  44. #include <FileMgr.h>
  45. #include <ToolboxUtil.h>
  46. #include <ControlMgr.h>
  47. #include <OSUtil.h>
  48. #include <ResourceMgr.h>
  49. #include <modified_MultiFinder.h>
  50.  
  51. /*     Globals    */
  52.                                                                                 
  53. typedef    struct ConfigurationItem {
  54.     int            value;
  55.     Handle        valueHandle;
  56.     MenuHandle    menuHandle;
  57. }    ConfigurationItem;
  58.  
  59. typedef    struct    ConfigurationTriplet {
  60.     ConfigurationItem    ceiling;
  61.     ConfigurationItem    interval;
  62.     ConfigurationItem    width;
  63. };
  64.  
  65. struct    ConfigurationTriplet    theConfiguration[4];
  66.  
  67. long            ticksGoneBy, tickSum[4], tickSumDivider[4];
  68. int                **windowPoshHandle, **windowPosvHandle;
  69. GrafPtr            theCurrentPort;
  70. MenuHandle        deskMenu, fileMenu, editMenu, configureMenu;
  71. WindowPtr        loadWindow;
  72. Str255            daName;
  73. Rect            dragBoundsRect;
  74.  
  75. /*    routines...    */
  76.  
  77. /*    restartProc    */
  78. restartProc()
  79. {
  80.     quitTime();
  81. }
  82.  
  83. /* the Switches  */
  84.  
  85. long    CeilingSwitch (itemNumber)
  86.     int        itemNumber;
  87. {
  88.     long    returnVal;
  89.     
  90.     switch(itemNumber){
  91.         case 1:
  92.             returnVal = 1;
  93.             break;
  94.         case 2:
  95.             returnVal = 2;
  96.             break;
  97.         case 3:
  98.             returnVal = 3;
  99.             break;
  100.         case 4:
  101.             returnVal = 4;
  102.             break;
  103.         case 5:
  104.             returnVal = 5;
  105.             break;
  106.         case 6:
  107.             returnVal = 6;
  108.             break;
  109.         case 7:
  110.             returnVal = 7;
  111.             break;
  112.         case 8:
  113.             returnVal = 8;
  114.             break;
  115.         case 9:
  116.             returnVal = 9;
  117.             break;
  118.         case 10:
  119.             returnVal = 10;
  120.             break;
  121.         case 11:
  122.             returnVal = 15;
  123.             break;
  124.         case 12:
  125.             returnVal = 20;
  126.             break;
  127.         case 13:
  128.             returnVal = 30;
  129.             break;
  130.         case 14:
  131.             returnVal = 60;
  132.             break;
  133.     }
  134.     
  135.     return(returnVal);
  136. }
  137.  
  138.  
  139. long    IntervalSwitch(itemNumber)
  140.     int    itemNumber;
  141. {
  142.     long    returnVal;
  143.     
  144.     switch(itemNumber){
  145.         case 1:
  146.             returnVal = 1;
  147.             break;
  148.         case 2:
  149.             returnVal = 2;
  150.             break;
  151.         case 3:
  152.             returnVal = 5;
  153.             break;
  154.         case 4:
  155.             returnVal = 10;
  156.             break;
  157.         case 5:
  158.             returnVal = 20;
  159.             break;
  160.         case 6:
  161.             returnVal = 60;
  162.             break;
  163.         case 7:
  164.             returnVal = 120;
  165.             break;
  166.         case 8:
  167.             returnVal = 180;
  168.             break;
  169.         case 9:
  170.             returnVal = 600;
  171.             break;
  172.         case 10:
  173.             returnVal = 1200;
  174.             break;
  175.     }
  176.  
  177.     return(returnVal);
  178. }
  179.  
  180. long    WidthSwitch(itemNumber)
  181.     int        itemNumber;
  182. {
  183.     long    returnVal;
  184.  
  185.     switch(itemNumber){
  186.         case 1:
  187.             returnVal = 1;
  188.             break;
  189.         case 2:
  190.             returnVal = 2;
  191.             break;
  192.         case 3:
  193.             returnVal = 3;
  194.             break;
  195.         case 4:
  196.             returnVal = 4;
  197.             break;
  198.         case 5:
  199.             returnVal = 5;
  200.             break;
  201.     }
  202.     return(returnVal);
  203. }
  204.  
  205.  
  206. /* doMessage */
  207.  
  208. doMessage (message0,message1,message2,message3)
  209. Str255    message0;
  210. Str255    message1;
  211. Str255    message2;
  212. Str255    message3;
  213. {                                    
  214. DialogPtr    dialogP;
  215. int            item;
  216.  
  217.         ParamText(message0, message1, message2, message3);
  218.         dialogP = GetNewDialog(MessageDialog, (Ptr)NIL, (Ptr)-1);
  219.         if (dialogP == NIL) {
  220.                 SysBeep(5);
  221.                 ExitToShell();
  222.                 }
  223.         else {
  224.                 InitCursor(); 
  225.                 ModalDialog((Ptr)NIL, item);
  226.                 DisposDialog(dialogP); 
  227.             }
  228.         
  229. }
  230.  
  231. /* setUpWorld */
  232.  
  233. setUpWorld()
  234. {
  235.     int            i;
  236.     SysEnvRec    theWorld;
  237.     OSErr        err;
  238.     
  239.     InitGraf(&thePort);
  240.     InitFonts();
  241.     FlushEvents(everyEvent,nullStopMask);
  242.     InitWindows();
  243.     InitMenus();
  244.     TEInit();
  245.     InitDialogs(restartProc);                                        
  246.     InitCursor();
  247.  
  248.     /*    check if WNE is implemented, and exit if not
  249.         ...stolen from Tech Note #158                */
  250.     err = SysEnvirons(1, &theWorld);
  251.     if ((theWorld.machineType < 0) ||
  252.         (NGetTrapAddress(WNETrapNum,ToolTrap) == NGetTrapAddress(UnImplTrapNum,ToolTrap))) {
  253.             doMessage("\pMultifinder not active","","","");
  254.             ExitToShell();
  255.         }
  256.     SetRect(&dragBoundsRect,
  257.         screenBits.bounds.left +4,
  258.         screenBits.bounds.top + MBarHeight,        /*    menubar height might change...    */
  259.         screenBits.bounds.right -4,
  260.         screenBits.bounds.bottom -4);
  261.  
  262.     
  263.     /*    get saved resources    & don't let 'em get purged    */
  264.  
  265.     OpenResFile("\pGLA appl"); /* note this is not nice! */
  266.     
  267.     for (i = 0; i < 4; i++) {
  268.         theConfiguration[i].ceiling.valueHandle = GetResource((ResType) 'INFO', (BASE_ID + i*3));
  269.         HNoPurge(theConfiguration[i].ceiling.menuHandle);
  270.         theConfiguration[i].ceiling.value = CeilingSwitch(**theConfiguration[i].ceiling.valueHandle);
  271.         
  272.         theConfiguration[i].interval.valueHandle = GetResource((ResType) 'INFO', (BASE_ID + 1 + i*3));
  273.         HNoPurge(theConfiguration[i].interval.menuHandle);
  274.         theConfiguration[i].interval.value = IntervalSwitch(**theConfiguration[i].interval.valueHandle);
  275.  
  276.         theConfiguration[i].width.valueHandle = GetResource((ResType) 'INFO', (BASE_ID + 2 + i*3));
  277.         HNoPurge(theConfiguration[i].width.menuHandle);
  278.         theConfiguration[i].width.value = WidthSwitch(**theConfiguration[i].width.valueHandle);
  279.     }
  280.  
  281.     /*    call ResError() after these, and set them manually if get error        */
  282.     windowPoshHandle = (int **) GetResource( (ResType) 'INFO', WINDOW_POSh);
  283.     HNoPurge(windowPoshHandle);
  284.     
  285.     windowPosvHandle = (int **) GetResource( (ResType) 'INFO', WINDOW_POSv);
  286.     HNoPurge(windowPosvHandle);
  287.     
  288.     /*    if point not inside dragBoundsRect, set it to default    */
  289.     if ((**windowPoshHandle < dragBoundsRect.left) ||
  290.         (**windowPoshHandle > dragBoundsRect.right) ||
  291.         (**windowPosvHandle > dragBoundsRect.bottom) ||
  292.         (**windowPosvHandle < dragBoundsRect.top)) {
  293.         
  294.         **windowPoshHandle = (int) ((dragBoundsRect.right - dragBoundsRect.left)/2);
  295.         **windowPosvHandle = (int) ((dragBoundsRect.bottom - dragBoundsRect.top)/2);
  296.     }
  297.     
  298.     loadWindow = GetNewWindow(MYWINDOW, (Ptr) NIL, (Ptr) -1);
  299.     MoveWindow(loadWindow, **windowPoshHandle, **windowPosvHandle, FALSE);
  300.     ShowWindow(loadWindow);
  301.  
  302.     SetPort(loadWindow);
  303.     PenNormal();
  304.  
  305. }
  306.  
  307.  
  308. /* setUpMenus */
  309.  
  310. setUpMenus()
  311. {
  312.     int        i;
  313.     
  314.     deskMenu = GetMenu(DESK_ID);
  315.     AddResMenu(deskMenu, 'DRVR');
  316.     InsertMenu(deskMenu, 0);
  317.     
  318.     fileMenu = GetMenu(FILE_ID);
  319.     InsertMenu(fileMenu, 0);
  320.     
  321.     editMenu = GetMenu(EDIT_ID);
  322.     InsertMenu(editMenu, 0);
  323.     
  324.     configureMenu = GetMenu(CONFIGURE_ID);
  325.     InsertMenu(configureMenu, 0);
  326.     
  327.     for (i = 0; i < 4; i++) {
  328.  
  329.         theConfiguration[i].ceiling.menuHandle = GetMenu(BASE_ID + i*3);
  330.         InsertMenu(theConfiguration[i].ceiling.menuHandle, -1);
  331.         CheckItem(theConfiguration[i].ceiling.menuHandle, (int) **theConfiguration[i].ceiling.valueHandle, TRUE);
  332.         
  333.         theConfiguration[i].interval.menuHandle = GetMenu(BASE_ID + i*3 + 1);
  334.         InsertMenu(theConfiguration[i].interval.menuHandle, -1);
  335.         CheckItem(theConfiguration[i].interval.menuHandle, (int) **theConfiguration[i].interval.valueHandle, TRUE);
  336.         
  337.         theConfiguration[i].width.menuHandle = GetMenu(BASE_ID + i*3 + 2);
  338.         InsertMenu(theConfiguration[i].width.menuHandle, -1);
  339.         CheckItem(theConfiguration[i].width.menuHandle, (int) **theConfiguration[i].width.valueHandle, TRUE);
  340.     }
  341.     
  342.     DrawMenuBar();
  343. }
  344.  
  345.  
  346.  
  347. /* doMenu */
  348.  
  349. doMenu(menuResult)
  350.     long    menuResult;
  351. {
  352.     int            menuID, itemNumber, newHPos, newVPos, itemHit, i, configSetting;
  353.     long        longPointer;
  354.     ProcPtr        myFilterProc;
  355.  
  356. /*    if (menuResult == (long) NIL)
  357.         menuResult = MenuChoice();
  358. */        
  359.     menuID        = HiWord(menuResult);
  360.     itemNumber    = LoWord(menuResult);
  361.     
  362.     switch(menuID)
  363.         {
  364.         case DESK_ID:            /*    Apple Menu        */
  365.             if (itemNumber == 1){
  366.                 Alert(MYALERT, NIL);
  367.                 break;
  368.             }
  369.             else if (itemNumber == 0)    /* b/c of heirarchical menus...    */
  370.                 break;
  371.             else
  372.             {
  373.                 GetItem(deskMenu, itemNumber, &daName);
  374.                 GetPort(&theCurrentPort);
  375.                 OpenDeskAcc(&daName);
  376.                 SetPort(theCurrentPort);
  377.                 
  378.                 break;
  379.             }
  380.             break;
  381.             
  382.         case FILE_ID:                /*     the File Menu    */
  383.             switch(itemNumber)
  384.                 {
  385.                 case 1:                /*    the print command    */
  386.                     break;
  387.                 case 2:                /*    the quit command    */
  388.                     quitTime();
  389.                     break;
  390.             }
  391.             break;
  392.             
  393.         case EDIT_ID:                        /*    the Edit Menu    */
  394.             switch(itemNumber){
  395.                 case 1:                        /*    the undo command    */
  396.                     break;
  397.                 case 2:                        /*    just dashed lines    */
  398.                     break;
  399.                 case 3:                        /*    the cut command        */
  400.                     break;
  401.                 case 4:                        /*    the copy command    */
  402.                     break;
  403.                 case 5:                        /*    the paste command    */
  404.                     break;
  405.                 case 6:                        /*    the clear command    */
  406.                     break;
  407.             }
  408.             break;
  409.             
  410.         case CONFIGURE_ID:                    /*    the Configure Menu    */
  411.             switch(itemNumber){
  412.                 case 1:                        /*    Set Time:        Heirarchical    */
  413.                 case 2:                        /*    Average Over:    Heirarchical    */
  414.                 case 3:                        /*    Set Width:        Heirarchical    */
  415.                     break;
  416.                 case 4:                        /*    spacer    */
  417.                     break;
  418.                 case 5:                        /*    Set Time:        Heirarchical    */
  419.                 case 6:                        /*    Average Over:    Heirarchical    */
  420.                 case 7:                        /*    Set Width:        Heirarchical    */
  421.                     break;
  422.                 case 8:                        /*    spacer    */
  423.                     break;
  424.                 case 9:                        /*    Set Time:        Heirarchical    */
  425.                 case 10:                    /*    Average Over:    Heirarchical    */
  426.                 case 11:                    /*    Set Width:        Heirarchical    */
  427.                     break;
  428.                 case 12:                    /*    spacer    */
  429.                     break;
  430.                 case 13:                    /*    Set Time:        Heirarchical    */
  431.                 case 14:                    /*    Average Over:    Heirarchical    */
  432.                 case 15:                    /*    Set Width:        Heirarchical    */
  433.                     break;
  434.                 case 16:                    /*    spacer    */
  435.                     break;
  436.                 case 17:                    /*    compress juggler RAM...    */
  437.                     MFMaxMem(&longPointer);
  438.                     break;
  439.             } /* end switch */
  440.             break;
  441.             
  442.         case BASE_ID:
  443.         case (BASE_ID + 3):
  444.         case (BASE_ID + 6):                        
  445.         case (BASE_ID + 9):
  446.         
  447.             configSetting = ((menuID - BASE_ID)/3);
  448.             CheckItem(theConfiguration[configSetting].ceiling.menuHandle, (int) **theConfiguration[configSetting].ceiling.valueHandle, FALSE);
  449.             **theConfiguration[configSetting].ceiling.valueHandle = (SignedByte) itemNumber;
  450.             CheckItem(theConfiguration[configSetting].ceiling.menuHandle, (int) **theConfiguration[configSetting].ceiling.valueHandle, TRUE);
  451.             theConfiguration[configSetting].ceiling.value = CeilingSwitch(itemNumber);
  452.             break;
  453.  
  454.         case (BASE_ID + 1):
  455.         case (BASE_ID + 4):
  456.         case (BASE_ID + 7):                        
  457.         case (BASE_ID + 10):
  458.             configSetting = (menuID - BASE_ID - 1)/3;
  459.             CheckItem(theConfiguration[configSetting].interval.menuHandle, (int) **theConfiguration[configSetting].interval.valueHandle, FALSE);
  460.             **theConfiguration[configSetting].interval.valueHandle = (SignedByte) (itemNumber);
  461.             CheckItem(theConfiguration[configSetting].interval.menuHandle, (int) **theConfiguration[configSetting].interval.valueHandle, TRUE);
  462.             theConfiguration[configSetting].interval.value = IntervalSwitch(itemNumber);
  463.             break;
  464.                         
  465.         case (BASE_ID + 2):
  466.         case (BASE_ID + 5):
  467.         case (BASE_ID + 8):                        
  468.         case (BASE_ID + 11):
  469.             configSetting = (menuID - BASE_ID - 2)/3;
  470.             CheckItem(theConfiguration[configSetting].width.menuHandle, (int) **theConfiguration[configSetting].width.valueHandle, FALSE);
  471.             **theConfiguration[configSetting].width.valueHandle = (SignedByte) itemNumber;
  472.             CheckItem(theConfiguration[configSetting].width.menuHandle, (int) **theConfiguration[configSetting].width.valueHandle, TRUE);
  473.             theConfiguration[configSetting].width.value = WidthSwitch(itemNumber);
  474.             break;
  475.  
  476.     }
  477.     HiliteMenu(0);
  478. }
  479.  
  480.  
  481. /* doLoad */
  482.                     
  483.  
  484. doLoad()
  485. {
  486.     long        currentTicks, deltaTicks, barHeight, barWidth, leftEdge, rightEdge,
  487.                 topEdge, bottomEdge;
  488.     Rect        loadBar, eraseBar;
  489.     int            i;
  490.     
  491.  
  492.     /*    sample the load    */
  493.     currentTicks = TickCount();
  494.     deltaTicks = currentTicks - ticksGoneBy;
  495.     ticksGoneBy = currentTicks;
  496.  
  497.     /*    increment accumulators and draw load    */
  498.     for (i = 0; i < 4; i++) {
  499.         tickSum[i] += deltaTicks;
  500.         tickSumDivider[i]++;
  501.         if (tickSum[i] > theConfiguration[i].interval.value) {
  502.     
  503.             leftEdge = 5 + (i * BAROFFSET);
  504.             bottomEdge = 35;                    /*    will become resizable    */
  505.             topEdge = 0;
  506.             barWidth = theConfiguration[i].width.value;
  507.         
  508.             barHeight = (40 - ((tickSum[i] * 35)/(tickSumDivider[i] * theConfiguration[i].ceiling.value)));        /*    arranged to eliminate decimals    */
  509.             SetRect (&loadBar, leftEdge, barHeight, leftEdge + barWidth, bottomEdge);    /*    left, top, right, bottom    */
  510.             SetRect (&eraseBar, leftEdge, topEdge, leftEdge + barWidth, barHeight);
  511.             EraseRect(&eraseBar);
  512.             PaintRect(&loadBar);
  513.     
  514.             tickSum[i] = 0;
  515.             tickSumDivider[i] = 0;
  516.         }    /*    endif    */
  517.     }    /*    end for    */
  518. }
  519.  
  520.  
  521. /* quitTime */
  522.  
  523.  
  524. quitTime()
  525. {                /*    time to write resources, etc.        */
  526.     int        i;
  527.     Point    p;
  528.     
  529.     for (i = 0; i < 4; i++) {
  530.         ChangedResource(theConfiguration[i].ceiling.valueHandle);
  531.         ChangedResource(theConfiguration[i].interval.valueHandle);
  532.         ChangedResource(theConfiguration[i].width.valueHandle);
  533.     }
  534.     
  535.     SetPort(loadWindow);
  536.     
  537.     p.h = 0;
  538.     p.v = 0;
  539.     
  540.     LocalToGlobal(&p);
  541.     
  542.     **windowPoshHandle = p.h;
  543.     **windowPosvHandle = p.v;
  544.  
  545.     ChangedResource((Handle) windowPosvHandle);
  546.     ChangedResource((Handle) windowPoshHandle);
  547.     
  548.     ExitToShell();
  549. }
  550.  
  551.  
  552.  
  553. /* main */
  554.  
  555.  
  556. main()
  557. {
  558.  
  559.     /*    variables    */
  560.         
  561.     EventRecord        theEvent;
  562.     Point            eventPt;
  563.     char            eventChar;
  564.     WindowPtr        whichWindow, eventWindow;
  565.     short            windowCode, stillInGoAway, controlCode, whichItem,
  566.                     scrapIndex;
  567.     
  568.     setUpWorld();
  569.     setUpMenus();
  570.     
  571.     
  572.     while (TRUE) {
  573.         WaitNextEvent(everyEvent, &theEvent, (long) NIL, (RgnHandle) NIL);
  574.         switch (theEvent.what){
  575.             case SUSPEND_RESUME_EVT:
  576.                 if ( ! BitTst( (Ptr) theEvent.message, SUSPEND_RESUME_BIT))        /*    suspend event    */
  577.                     ;
  578.                 else (BitTst( (Ptr) theEvent.message, SUSPEND_RESUME_BIT))        /*    resume event    */
  579.                     ;
  580.                 if ( ! BitTst( (Ptr) theEvent.message, CLIPBOARD_BIT))        /*    don't convert clipboard    */
  581.                     ;
  582.                 else (BitTst( (Ptr) theEvent.message, CLIPBOARD_BIT))        /*    convert clipboard    */
  583.                     ;
  584.                 break;
  585.             case mouseDown:
  586.                 eventPt = theEvent.where;
  587.                 windowCode = FindWindow(eventPt, &eventWindow);
  588.                 switch (windowCode) {
  589.                     case inSysWindow:
  590.                         SystemClick(&theEvent, eventWindow);
  591.                         break;
  592.                     case inContent:    /*    won't work on minor switch b/c of compatibility issues w/Excel    */
  593.                     case inGrow:
  594.                     case inGoAway:
  595.                         if (eventWindow == loadWindow)
  596.                             DragWindow(eventWindow, eventPt, &dragBoundsRect);
  597.                         break;
  598.                     case inDrag:
  599.                         DragWindow(eventWindow, eventPt, &dragBoundsRect);
  600.                         break;
  601.                     case inDesk:
  602.                             break;
  603.                     case inMenuBar:
  604.                         doMenu(MenuSelect(theEvent.where));
  605.                         break;
  606.                     default: 
  607.                         break;
  608.                 }    /*    end switch(windowCode)    */
  609.             break;
  610.                 
  611.             case keyDown:
  612.             case autoKey:
  613.                 eventChar = theEvent.message;
  614.                 if (theEvent.modifiers & cmdKey) {
  615.                     doMenu(MenuKey(eventChar));
  616.                     HiliteMenu(0);
  617.                 }
  618.                 break;
  619.             case activateEvt:
  620.                 break;
  621.             case updateEvt:        /*    absolutely necessary, else will not get nullEvents initially    */
  622.                 BeginUpdate(theEvent.message);
  623.                 EndUpdate(theEvent.message);
  624.                 break;
  625.             case nullEvent: 
  626.                 doLoad();
  627.                 break;
  628.             default: ;
  629.         } /* end of case theEvent.what */
  630.     } /* while */
  631. }